package com.app.community.controller;

import com.app.community.exception.ErrorCodeEnum;
import com.app.community.service.EmailSendService;
import com.app.community.service.ResetService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户重置密码验证
 * @Date 2020/10/24 21:51
 **/
@Slf4j
@Controller
public class ResetController {

    // 验证码
    Map<String,Integer> verificationCode  = new HashMap<>();

    @Autowired
    EmailSendService emailSendService;

    @Autowired
    ResetService resetService;

    @GetMapping("reset")
    public String getReset(){
        return "common/reset";
    }

    @PostMapping("resetEmail")
    public void resetEmail(@RequestParam("emailAddress")String email){
        // 验证码从service中获取
        verificationCode.put(email,emailSendService.sendResetEmail(email));
    }

    @PostMapping("reset")
    public String sendReset(@RequestParam("userId")String userId, @RequestParam("verificationCode")Integer code, @RequestParam(value = "password",required = false) String password, @RequestParam(value = "passwordRepeat",required = false) String passwordRepeat, Model model, HttpSession httpSession){
        model.addAttribute("userId",userId);
        model.addAttribute("password",password);

        if (userId == null || userId.isEmpty()){
            model.addAttribute("error", ErrorCodeEnum.INVALIDEMAIL.getDescription());
            return "common/reset";
        }

        if (password == null || password.isEmpty()){
            model.addAttribute("error",ErrorCodeEnum.INITIALPASSWORDERROR.getDescription());
            return "common/reset";
        }

        if (passwordRepeat == null || passwordRepeat.isEmpty()){
            model.addAttribute("error",ErrorCodeEnum.HAVENOTCONFIRMPASSWORD.getDescription());
            return "common/register";
        }

        if (!password.equals(passwordRepeat)){
            model.addAttribute("error",ErrorCodeEnum.PASSWORDNOTMATCH.getDescription());
            return "common/reset";
        }

        if (code == null){
            model.addAttribute("error",ErrorCodeEnum.EMPTYVERTIFICATIONCODE.getDescription());
            return "common/reset";
        }

        if (!code.equals(verificationCode.get(userId))){
            model.addAttribute("error",ErrorCodeEnum.ERRORVERTIFICATIONCODE.getDescription());
            return "common/reset";
        }

        resetService.resetUser(userId,password);

        log.info("用户:" + userId + "修改密码成功！");
        httpSession.removeAttribute("userId");
        return "redirect:login";
    }

}
